package cn.edu.tsinghua.thss.alg.closestpair.gui;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JFrame;

import cn.edu.tsinghua.thss.alg.closestpair.Point;

/**
 * The panel that paints all the points and the last line.
 * 
 * @author
 * 
 */
public class PointsPanel extends Canvas implements MouseListener,
		MouseMotionListener {
	private JFrame frame;

	/** The coordinates of the closest pair */
	private int x1, y1, x2, y2;

	/** The points that have been entered by the mouse */
	private ArrayList<Point> points;

	public Point[] getPoints() {
		Point[] pts = new Point[points.size()];
		for (int i = 0; i < points.size(); i++)
			pts[i] = points.get(i);
		return pts;
	}

	/** If all points have been entered */
	boolean finished = false;

	public PointsPanel(JFrame frame) {
		this.frame = frame;
		this.points = new ArrayList<Point>();
		this.setBackground(Color.WHITE);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}

	/**
	 * Draws a line between two given points.
	 * 
	 * @param x1
	 *            the x coordinate of the first point
	 * @param y1
	 *            the y coordinate of the first point
	 * @param x2
	 *            the x coordinate of the second point
	 * @param y2
	 *            the y coordinate of the second point
	 */
	public void drawLine(double x1, double y1, double x2, double y2) {
		this.x1 = (int) x1;
		this.x2 = (int) x2;
		this.y1 = (int) y1;
		this.y2 = (int) y2;
		finished = true;
		repaint();
	}

	/**
	 * Paint points and lines. Overrides.
	 */
	public void paint(Graphics g) {
		super.paint(g);

		if (!finished) {
			// If not finished, continue drawing points
			g.setColor(Color.BLACK);
			for (Point p : points)
				g.fillOval((int) p.getX(), (int) p.getY(), 2, 2);
			frame.validate();
		} else {
			// If finished, draw a line between the two closest points
			for (Point p : points)
				g.fillOval((int) p.getX(), (int) p.getY(), 2, 2);
			g.drawLine(x1, y1, x2, y2);
			frame.validate();
			finished = false;
		}
	}

	/**
	 * When the mouse is clicked, add the point
	 */
	public void mouseClicked(MouseEvent e) {
		System.out.println("Click:" + "(" + e.getX() + "," + e.getY() + ")");
		points.add(new Point(e.getX(), e.getY()));
		repaint();
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Show the point where the mouse is pressed
	 */
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Show the position of the mouse in the status bar when it moves around.
	 */
	public void mouseMoved(MouseEvent e) {
		((ClosestPairFrame) frame).getStatusBar().setMessage(
				"(" + e.getX() + "," + e.getY() + ")");
		frame.validate();
	}
}
